package com.eatme.leetcode.problems;

import com.eatme.core.tree.segmenttree.SegmentTree;

/**
 * 303. 区域和检索 - 数组不可变 - range-sum-query-immutable
 */
class NumArray303 {

    private SegmentTree<Integer> segmentTree;

    public NumArray303(int[] nums) {
        if (nums.length>0) {
            Integer[] data = new Integer[nums.length];
            for (int i = 0; i < nums.length; i++)
                data[i] =nums[i];
            segmentTree = new SegmentTree<>(data,(a,b)-> a+b);

        }
    }

    public int sumRange(int i,int j) {
        if (segmentTree==null)
            throw  new IllegalArgumentException("Segment Tree is null");
        return segmentTree.query(i,j);
    }
}
